<?php
namespace app\user\controller;
use app\admin\model\user\User;
use app\common\controller\Home;
use think\facade\Cache;
use think\facade\Cookie;
use think\facade\Session;
class Register extends Home
{
    public function index()
    {
        if($this->request->post())
        {
            $input = $this->request->post();
            //用户协议检测
            if(!isset($input['iserxieyi'])) $this->error("请确保你已经阅读用户协议，并且勾选");
            //空用户
            if(!$input['username']) $this->error("请输入账户，或者手机号");
            //空密码
            if(!$input['password']) $this->error("请输入密码");
            //nc_token
            //if($input['nc_token']!=session("NC_TOKEN") || !$input['nc_token']) $this->error("请滑动验证条获取验证码");
            //短信验证码
            //if($input['sms_code']!=cache("sms_code_".$input['username'])) $this->error("请输入正确的短信验证码");
            //邮箱验证码
            if($input['email_code']!=cache($input['username']."_email_code")) $this->error("请输入正确的邮箱验证码");

            $user = (new User())->where("username",$input['username'])->find();

            if($user) $this->error("该用户已存在请稍后再试");

            $input['password'] = Passwordmd5($input['password']);

            if(valiDatePhone($input['username'])) $input['phone'] = $input['username'];
            if(valiDateEmail($input['username'])) $input['email'] = $input['username'];

            $res = User::create($input);

            if($res)
            {
                unset($res['password']);
                cookie("user",$res);
               /* Session::delete("NC_TOKEN");
                Cache::clear("sms_code");*/
                Cache::clear($input['username']."_email_code");
                cmf_generate_user_token($res["id"], 'web');
                $this->success("注册成功",'',$input);
            }
            $this->error("注册失败");
        }
        return view();
    }

    public function find_pwd()
    {
        if($this->request->post())
        {
            $input = $this->request->post();
            //空用户
            if(!$input['username']) $this->error("请输入账户，或者手机号");
            //空密码
            if(!$input['password']) $this->error("请输入密码");
            //nc_token
            //if($input['nc_token']!=session("NC_TOKEN") || !$input['nc_token']) $this->error("请滑动验证条获取验证码");
            //短信验证码
            //if($input['sms_code']!=cache("sms_code_".$input['username'])) $this->error("请输入正确的短信验证码");
            //邮箱验证码
            if($input['email_code']!=cache($input['username']."_email_code")) $this->error("请输入正确的邮箱验证码");

            $user = (new User())->where("username",$input['username'])->find();

            if(!$user) $this->error("该用户不存在请稍后再试");

            $res = User::where("username",$input['username'])
                ->update([
                    "password" => Passwordmd5($input['password'])
                ]);
            if($res)
            {
                /*session("user",$user);
                Session::delete("NC_TOKEN");
                Cache::clear("sms_code");*/
                unset($res['password']);
                cookie("user",$user);
                Cache::clear($input['username']."_email_code");
                cmf_generate_user_token($user["id"], 'web');
                $this->success("修改成功",'',$user);
            }
            $this->error("注册失败");
        }
        return view();
    }
}